$(function(){
	eachtypeChange();
	//初始化地区
	var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
	}
	//初始化	
	initFileInput("file_upload", urlcore + "/api/attachment/upload","#img_details");
	initMySummernote("content");
	//外部js调用
    laydate({
        elem: '#startTime', //需显示日期的元素选择器
		event: 'focus', //触发事件
		format: 'YYYY-MM-DD hh:mm:ss', //日期格式
		istime: true, //是否开启时间选择
		isclear: true, //是否显示清空
		istoday: true, //是否显示今天
		issure: true, //是否显示确认
		festival: true, //是否显示节日
		min:'1900-01-01 00:00:00',//最小日期
		max:'2099-12-31 23:59:59',//最大日期
		start:laydate.now(),    //开始日期
		key:'2017-01-17 23:22:59'
		//choose: function(dates){} //选择好日期的回调
    });    
    //外部js调用
    laydate({
        elem: '#endTime', //需显示日期的元素选择器
		event: 'focus', //触发事件
		format: 'YYYY-MM-DD hh:mm:ss', //日期格式
		istime: true, //是否开启时间选择
		isclear: true, //是否显示清空
		istoday: true, //是否显示今天
		issure: true, //是否显示确认
		festival: true, //是否显示节日
		min:'1900-01-01 00:00:00',//最小日期
		max:'2099-12-31 23:59:59',//最大日期
		start:laydate.now(),    //开始日期
		key:'2017-01-17 23:22:59'
		//choose: function(dates){} //选择好日期的回调
    });
	
    var id = getvl("id");
    var type = getvl("type");
    if(type==3){
    	$('#hide').css('display','none');
    }
    if (id != '') {
        $('#thisId').text(id);
        $('#update_add_title').text("修改广告");
        $('#thisType').css("display", "none");
        $('#eachtype').val(type);
        $('#jiu_picture_div').css("display", "block");    
	    $.ajax({
	        url: urlcore + "/api/eachPicture/details?id="+id,
	        type: "get",
	        dataType: 'json',
	        contentType: "application/json;charset=utf-8",
	        success:function(data){
	            if (data.success == true) {
	            	//地区
	            	$("#change option[value="+data.data.areaCode+"]").prop("selected", 'selected'); 
	            	
	                $('#linkUrl').val(data.data.linkUrl);
	                $('#startTime').val(new Date(data.data.startTime).pattern("yyyy-MM-dd hh:mm:ss"));
	               	$('#endTime').val(new Date(data.data.endTime).pattern("yyyy-MM-dd hh:mm:ss"));
	                var attachmentUrl = data.data.attachmentUrl;
	                var imgStr = '';
	                if (attachmentUrl != null && attachmentUrl != '') {
	                    var imgarr = attachmentUrl.split("***");
	                    var imgStr2 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr2 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#img_details').html(imgStr2);
	                $('#title').val(data.data.title);
	                $('#advertType').val(data.data.advertType);
                    $('#content').summernote("code",data.data.content);

	            } else if (data.code == 'OVERTIME'){
	                var thisUrl = window.location.href;
	                if (thisUrl.indexOf('login.html') <= -1) {
	                    top.window.location.href="login.html";
	                }
	            } else {
	                alert(data.msg);
	            }
	        },
	        error:function() {
	            /* Act on the event */
	            alert("error");
	        }
	    });
	}
});


//添加
function subMsg(){
    var flag = $('#commentForm').valid();
    if (!flag) { return; }
    var id = $('#thisId').text();
    
   	//地区
	var content = $("#change").find("option:selected").text();
	var arr = content.split(' ');
	var provinceName = arr[0];
	var cityName = arr[1];
	var areaName = arr[2];
	var areaCode = $("#change").val();

	if (areaCode=='' || areaCode==undefined || areaCode=='undefined') {
            alert('请选择完整地区');
            return;
	}

    if (provinceName.indexOf("市") > -1 || provinceName.indexOf("香港") > -1 || provinceName.indexOf("澳门") > -1) {
        cityName = provinceName;
    }
    //类型
   	var eachtype = $("#eachtype")[0].options[$("#eachtype")[0].selectedIndex].value;
   	var advertType="";
   	if(eachtype!=3){
   		 advertType = $("#advertType").val();
   	}

    //图片地址
    var attachmentUrl = "";
    $("#img_details img").each(function(index,item){
		attachmentUrl = $(this).attr("src")+"***";
	});
    if (attachmentUrl=='' && id=='') {
        alert('请上传图片');
        return;
    }
    attachmentUrl = attachmentUrl.substring(0, attachmentUrl.length-3);
    //链接地址
    var linkUrl = $('#linkUrl').val();

    //有效期
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();


    var title = $('#title').val();
    if (title=='') {
        alert('标题不能为空');
        return;
    }
	var content = $('#content').summernote('code');
    if (content=='') {
        alert('内容不能为空');
        return;
    }
    var startTime2 = Date.parse(new Date(startTime));
    var endTime2 = Date.parse(new Date(endTime));
    $.ajax({
        url:  urlcore + "/api/eachPicture/addEachPicture",
        type: "post",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "id": id,
            "type": eachtype,
            "advertType": advertType,
            "attachmentUrl": attachmentUrl,
            "title": title,
            "destatus":0,
            "content": content,
            "linkUrl": linkUrl,
            "startTime": startTime2, 
            "endTime": endTime2,
            "areaCode": areaCode,
            "province": provinceName,
            "city": cityName,
            "area": areaName       
        }),
        dataType: "json",
        async: false,
        success:function(data){
            if (data.success == true) {
                if (id=="") {
                    if (confirm("添加成功，是否继续添加？")) {
                        //刷新
                        location.reload();
                    } else {
                        //跳转列表页
                        window.location.href = "each_picture_list.html";
                    }
                }else{
                    alert("修改成功");
                    //跳转列表页
                    window.location.href = "each_picture_list.html";
                }
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
}

function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}

//下拉框隐藏
function eachtypeChange(){
     $("#eachtype").change(function () {
   		if($("#eachtype").val()==3){
   			$('#hide').css('display','none');
   		}else{
   			$('#hide').css('display','block');
   		}
	 });
}
